<?php

class TestAnalyzer
{
  static public function analyze(array $data)
  {
    if (!count($data)) return array(null, null);
    $elements = array(
      '1' => 0,
      '2' => 0,
      '3' => 0,
      '4' => 0,
      '5' => 0
    );
    $data = array_filter($data);
    if (!count($data)) return array(null, null);
    $data = array_keys($data);
    $questions = self::findQuestionsByIds($data);
    foreach ($questions as $question) {
      if ($question) {
        $elements[$question->getElementId()]++;
      }        
    }
    arsort($elements);
    $tmp = array();
    foreach ($elements as $key => $val) {
      $tmp[] = array(
        'id' => $key,
        'count' => $val
      );
    }
    if ($tmp[0]['count'] === $tmp[1]['count'] && $tmp[2]['count'] === $tmp[1]['count']) {
      return array(null, null);
    }
    if ($tmp[1]['count'] === 0) {
      return array($tmp[0]['id'], null);
    }
    return array($tmp[0]['id'], $tmp[1]['id']);
  }

  static protected function findQuestionsByIds($data)
  {
    return Doctrine::getTable('Question')
      ->createQuery()
      ->whereIn('id', $data)
      ->execute();
  }
}
